/*
 * 
 */
package traductor.jsps.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.orm.PersistentException;

import traductor.jsps.servlets.vo.VOpalabra;
import traductor.jsps.servlets.vo.VOtraduccion;
import traductor.jsps.servlets.vo.VOLogin;



// TODO: Auto-generated Javadoc
/**
 * Servlet implementation class loginServlet.
 */
public class loginServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new login servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
		
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println("Usuario: " + username);
		System.out.println("Password: " + password);
		
		if(username.equals("admin") && password.equals("admin")){
			System.out.println("Bienvenido Usuario: " + username);
			
			VOLogin vo = new VOLogin();
			vo.setUsername(username);
			vo.setPassword(password);
			
			HttpSession session = request.getSession(true); 
			
			List<VOLogin> list = new ArrayList<VOLogin>();
			List<VOpalabra> lista = new ArrayList<VOpalabra>();
			
			session.setAttribute("usuarioLogeado", vo);
			session.setAttribute("listaEspanol", lista);
			session.setAttribute("listaMapudungun", lista);
			
			request.getRequestDispatcher("welcome.jsp").forward(request, response);
			
		} else {
			JOptionPane.showMessageDialog(null, "Error de acceso\nVerifique su Usuario y Password ingresados");
			response.sendRedirect("login.html");
		}	
	}

}
